<?php
namespace app\flash\controller;
use QL\QueryList;
use think\Db;
use think\Controller;
use think\Exception;
use think\Validate;

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: *");
//header('Access-Control-Allow-Methods: POST,GET','OPTIONS');
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http动作
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
    exit;
}
class UserComment extends Base
{
   //发布评论
    public function comment(){
        $data = input('');
        $data['from_id'] = $this->user['id'];
        $validate = Validate::make([
            'message_id'  => 'require',
            'content' => 'require',
            'flag'=>'require'
        ]);
        $res = $validate->check($data);
        if($res !== true){
            return json(msg(0, '', $validate->getError()));
        }
        $flag= $data['flag'];
        if($flag==1){
            $tableName = 'flashs';
        }elseif($flag==2){
            $tableName = 'deepness';
        }elseif($flag==3){
            $tableName = 'friend_message';
        } else{
            return json(msg(0, '', 'flag非法'));
        }
        $search      = array(" ","　","\n","\r","\t");
        $replace     = array("","","","","");
        $content = $data['content'];
        $data['content'] = json_encode(str_replace($search,$replace,$content));
        $data['from_id'] = $this->user['id'];
        $message = Db::table("$tableName")->where('id',$data['message_id'])->find();
        if(count($message)==0){
            return json(msg(0,'','未找到该文章'));
        }
        Db::startTrans();
        $res = Db::table('comment')->insertGetId($data);
        $zpls = Db::table("$tableName")->where('id',$data['message_id'])->setInc('pinglun_no');
        if ($res&&$zpls) {
            //该条文章的评论回复数自增
            Db::commit();
            $result = Db::table('comment')
                ->alias('c')
                ->join('user u','u.id = c.from_id','left')
                ->field('c.id,c.from_id,c.message_id,c.content,u.username,u.phone,like_count,u.picture,c.create_time,c.reply_num')
                ->where('c.id',$res)
                ->find();
            $result['content'] = json_decode($result['content']);
            return json(msg(1,$result,'评论成功'));
        }else{
            Db::rollback();
            return json(msg(0,'','评论失败'));
        }
    }

    //发表回复
    public function reply(){
        $data = input('');
        $data['from_id'] = $this->user['id'];
        $validate = Validate::make([
            'cid'  => 'require',    //楼层对应评论id
            'content' => 'require',
        ]);
        $res = $validate->check($data);
        if($res !== true){
            return json(msg(0, '', $validate->getError()));
        }
        $search      = array(" ","　","\n","\r","\t");
        $replace     = array("","","","","");
        $content = $data['content'];
        $data['content'] = json_encode(str_replace($search,$replace,$content));
        $data['from_id'] = $this->user['id'];
        $p = Db::table('comment')->where(array('id'=>$data['cid']))->find();
        if(empty($p)){
            return json(msg(0,'','未找到该楼层数据'));
        }
        if(!empty($data['parent_id'])){
            $pa = Db::table('reply')->where('id',$data['parent_id'])->find();
            $data['to_id'] = $pa['from_id'];
        }else{
            $data['to_id'] = $p['from_id'];
        }
        if($p['flag']==1){
            $tableName = 'flashs';
        }elseif($p['flag']==2){
            $tableName = 'deepness';
        }elseif($p['flag']==3){
            $tableName = 'friend_message';
        } else{
            return json(msg(0, '', 'flag非法'));
        }
        Db::startTrans();
        $res = Db::table('reply')->insertGetId($data);
        $zpls = Db::table("$tableName")->where('id',$p['message_id'])->setInc('pinglun_no');
        $comment = Db::table('comment')->where(array('id'=>$data['cid']))->setInc('reply_num');
        if ($res&&$zpls&&$comment) {
            Db::commit();
            $result = Db::table('reply')
                ->alias('r')
                ->join('user u','u.id = r.from_id','left')
                ->join('user p','p.id = r.to_id','left')
                ->join('reply pr','r.parent_id = pr.id','left')
                ->field('r.id,r.from_id,r.cid,r.content,r.parent_id,u.username,u.phone,r.like_count,u.picture,r.create_time,p.username as p_username,p.phone as p_phone,p.picture as p_picture,pr.content as p_content')
                ->where('r.id',$res)
                ->find();
            if($result['parent_id']==0){
                $result["p_username"] = null;
                $result["p_phone"] = null;
                $result["p_picture"] = null;
                $result["p_content"] = null;
            }
            $result['content'] = json_decode($result['content']);
            return json(msg(1,$result,'回复成功'));
        }else{
            Db::rollback();
            return json(msg(0,'','回复失败'));
        }
    }
}
